package com.dcd.service.impl;

import com.dcd.mapper.InformationMapper;
import com.dcd.pojo.Information;
import com.dcd.service.InformationService;
import com.dcd.util.SolrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InformationServiceImpl implements InformationService {

    @Autowired
    private SolrUtil solrUtil;
    @Autowired(required = false)
    private InformationMapper im;

    /**
     * 查询所有资讯
     *
     * @param pageZiXunIndex 当前页数
     * @return 所有资讯信息
     */
    @Override
    public Map<String, Object> selInformation(Integer pageZiXunIndex) {
        Map<String, Object> map = new HashMap<>();
        Page<Information> page = PageHelper.startPage(pageZiXunIndex, 16);
        im.selInformation();
        map.put("Total", page.getTotal());   //总共多少条数据
        map.put("Pages", page.getPages());   //总页数
        map.put("PageNum", page.getPageNum());   //当前页数
        map.put("ziXunList", page.getResult());   //所有资讯信息
        return map;
    }

    /**
     * 根据资讯id查询单个资讯详情
     * 修改资讯阅读次数
     * @param information_id 资讯id
     * @return 返回单个对象  查询一个资讯信息
     */
    @Override
    @Transactional
    public Information singleInformation(Integer information_id) {
        //这里代表已经在资讯页面了 需要修改资讯阅读次数
        im.update_information_count(information_id);
        return im.singleInformation(information_id);
    }

    /**
     * 查询前十的热搜榜
     *
     * @return 前十的数据
     */
    @Override
    public List<Information> tenInfoAll() {
        return im.tenInfoAll();
    }


    /**
     * 查询首页最新24条资讯
     *
     * @return 返回 24条资讯 不需要分页
     */
    @Override
    public List<Information> select_index_information() {
        return im.select_index_information();
    }

    /**
     * 根据车系id查询此系列的所有资讯
     *
     * @param car_series_id 获取车系id
     * @return 返回同车系所有资讯
     */
    @Override
    public List<Information> seleCarSeriesInformation(Integer car_series_id) {
        return im.seleCarSeriesInformation(car_series_id);
    }

    /**
     * 搜索查询资讯
     *
     * @param solr_content 搜索文本内容
     * @param page_index   当前页数
     * @return
     */
    @Override
    public Map<String, Object> select_information_by_solr(String solr_content, int page_index) throws IOException, SolrServerException {
        HashMap<String, Object> conditions = new HashMap<>();
        conditions.put("solr_content", solr_content);
        conditions.put("collection", "dcd_information");
        conditions.put("highlight_field", "information_title");
        conditions.put("page_index", page_index);
        conditions.put("all_fields", new String[]{"id", "information_id", "information_release_date", "information_read_count", "information_title", "information_images_1", "user_name", "user_id", "user_head_portrait_url"});
        return solrUtil.solr_content_by_collection(conditions);
    }

    /**
     * 查询最近一周的最新资讯
     *
     * @return
     */
    @Override
    public List<Information> select_week() {
        return im.select_week();
    }
}
